package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.utils.bean.DateRanger;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.domain.SysOperLogFetcher;
import com.ruoyi.system.domain.SysOperLogTable;
import com.ruoyi.system.domain.dto.SysOperLogExportExcel;
import com.ruoyi.system.domain.dto.SysOperLogListInput;
import com.ruoyi.system.service.ISysOperLogService;
import lombok.RequiredArgsConstructor;
import org.babyfish.jimmer.Page;
import org.babyfish.jimmer.sql.JSqlClient;
import org.babyfish.jimmer.sql.ast.mutation.SaveMode;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
@RequiredArgsConstructor
public class SysOperLogServiceImpl implements ISysOperLogService {
//    @Autowired
//    private SysOperLogMapper operLogMapper;

    private final JSqlClient sqlClient;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        sqlClient.saveCommand(operLog).setMode(SaveMode.INSERT_ONLY).execute();
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLogListInput operLog, DateRanger dateRanger) {
        SysOperLogTable table = SysOperLogTable.$;
        return sqlClient.createQuery(table).where(
                        table.operIp().ilikeIf(operLog.getOperIp()),
                        table.operName().ilikeIf(operLog.getOperName()),
                        table.title().ilikeIf(operLog.getTitle()),
                        table.status().eqIf(operLog.getStatus()),
                        table.businessType().eqIf(operLog.getBusinessType()),
                        table.operTime().betweenIf(DateRanger.isAllNotEmpty(dateRanger),dateRanger.getBeginTime(),dateRanger.getEndTime())
                ).select(table.fetch(SysOperLogFetcher.$.allScalarFields()))
                .execute();
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(String[] operIds) {
        SysOperLogTable table = SysOperLogTable.$;
        return sqlClient.createDelete(table).where(table.operId().in(Arrays.asList(operIds))).execute();
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(String operId) {
        return sqlClient.findById(SysOperLog.class, operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        SysOperLogTable table = SysOperLogTable.$;
        sqlClient.createDelete(table).execute();
    }

    @Override
    public Page<SysOperLog> selectOperLogPage(SysOperLogListInput operLog, DateRanger dateRanger, PageQuery pageQuery) {
        SysOperLogTable table = SysOperLogTable.$;
        return sqlClient.createQuery(table).where(
                        table.operIp().ilikeIf(operLog.getOperIp()),
                        table.operName().ilikeIf(operLog.getOperName()),
                        table.title().ilikeIf(operLog.getTitle()),
                        table.status().eqIf(operLog.getStatus()),
                        table.businessType().eqIf(operLog.getBusinessType()),
                        table.operTime().betweenIf(DateRanger.isAllNotEmpty(dateRanger), dateRanger.getBeginTime(), dateRanger.getEndTime())
                ).select(table.fetch(SysOperLogFetcher.$.allScalarFields()))
                .fetchPage(pageQuery.getPageNum(), pageQuery.getPageSize());
    }


    @Override
    public List<SysOperLogExportExcel> selectOperLogExportList(SysOperLogListInput operLog, DateRanger dateRanger) {
        SysOperLogTable table = SysOperLogTable.$;
        return sqlClient.createQuery(table).where(
                        table.operIp().ilikeIf(operLog.getOperIp()),
                        table.operName().ilikeIf(operLog.getOperName()),
                        table.title().ilikeIf(operLog.getTitle()),
                        table.status().eqIf(operLog.getStatus()),
                        table.businessType().eqIf(operLog.getBusinessType()),
                        table.operTime().betweenIf(DateRanger.isAllNotEmpty(dateRanger), dateRanger.getBeginTime(), dateRanger.getEndTime())
                ).select(table.fetch(SysOperLogExportExcel.class))
                .execute();
    }
}
